package logic

import (
	"common"
	"errors"
	"models/dal/query"
	"time"
)

type CommonLogic struct {
	BaseLogicAbs
}

// NewCommonLogic
func NewCommonLogic() *CommonLogic {
	logic := CommonLogic{
		BaseLogicAbs{
			message: "",
		},
	}
	return &logic
}

// GetConfig 获取配置
func (cl *CommonLogic) GetConfig(keys ...string) (map[string]any, error) {
	var ret = make(map[string]any)
	ctx, cancel := common.WithTimeoutContext(time.Second * 2)
	defer cancel()
	configModel := query.Q.AdminConfig.WithContext(ctx)

	if len(keys) > 0 {
		configModel = configModel.Where(query.Q.AdminConfig.Name.In(keys...))
	}

	list, err := configModel.Find()
	if err != nil {
		return map[string]any{}, errors.New("系统错误")
	}
	for _, config := range list {
		if _, ok := ret[config.Name]; !ok {
			ret[config.Name] = config.Value
		}
	}
	return ret, nil
}
